package cn.golxen.rsms.redis;

import cn.golxen.rsms.bean.entity.GoodsMain;
import cn.golxen.rsms.bean.entity.GoodsStore;
import cn.golxen.rsms.bean.entity.GoodsType;
import cn.golxen.rsms.commons.utils.BarcodeUtils;
import cn.golxen.rsms.commons.utils.LocalDateTimeUtils;
import cn.golxen.rsms.exception.exceptionClass.GetBarcodeInfoException;
import cn.golxen.rsms.mapper.GoodsMapper;
import cn.golxen.rsms.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class GoodsRedis {

    @Autowired
    GoodsMain goodsMain;
    @Autowired
    GoodsStore goodsStore;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    @Qualifier(value = "taskExecutor")
    private ThreadPoolTaskExecutor poolTaskExecutor;


    @Cacheable(value = "goods_main",key = "#code")
    public GoodsMain getBarcodeInfo(String code) {
        log.info("获取商品基本信息GoodsMain:"+code);
        try {
            //获取商品基本信息
            goodsMain = goodsMapper.getGoodsMain(code);
            //如果商品不存在则试图去中国编码获取
            if (goodsMain == null) {
                GoodsMain goodsMain1 = new GoodsMain();
                String resGoods = BarcodeUtils.GetGoodsInfo(code);
                log.info(resGoods);
                JSONObject jsonObject2 = new JSONObject(resGoods).getJSONObject("showapi_res_body");

                if (!jsonObject2.getString("ret_code").equals("0"))
                    throw new GetBarcodeInfoException();

                goodsMain1.setCode(code);
                goodsMain1.setGoodsName(jsonObject2.getString("goodsName"));
                goodsMain1.setTrademark(jsonObject2.getString("trademark"));
                goodsMain1.setCreateDate(LocalDateTimeUtils.getLocalDateTime());
                if (!jsonObject2.getString("goodsType").equals("")) {
                    if (jsonObject2.getString("goodsType").split(">>").length==1)
                        goodsMain1.setGoodsType(jsonObject2.getString("goodsType"));
                    else
                        goodsMain1.setGoodsType(jsonObject2.getString("goodsType").split(">>")[1].split("、")[0]);
                } else
                    goodsMain1.setGoodsType("其他");
                if (!jsonObject2.getString("price").equals(""))
                    goodsMain1.setSuggestedPrice(Double.parseDouble(jsonObject2.getString("price")));
                if (!jsonObject2.getString("img").equals(""))
                    goodsMain1.setImage(jsonObject2.getString("img"));

                //获取到商品信息后开启新线程处理数据库操作
                poolTaskExecutor.execute(() -> {
                    log.info("异步处理添加GoodsMain和判断Type是否存在:"+code);
                    goodsMapper.addGoodsMain(goodsMain1);
                    redisUtils.timerSuggestedPrice(goodsMain1.getCode());
                    addGoodsTypeNx(goodsMain1.getGoodsType());
                });
                return goodsMain1;
            }
            redisUtils.timerSuggestedPrice(code);
            return goodsMain;
        } catch (Exception e) {
            e.printStackTrace();
            throw new GetBarcodeInfoException();
        }
    }

    @CacheEvict(value = "goods_main",key = "#code")
    public int updateGoodsMainSuggestedPrice(String code) {
        log.info("计算并同步建议价:"+code);
        return goodsMapper.updateGoodsMainSuggestedPrice(code);
    }

    @CacheEvict(value = "goods_type_list")
    public void addGoodsTypeNx(String type) {
        if (goodsMapper.isExGoodsTypeByType(type) == null) {
            goodsMapper.addGoodsType(type);
        }
    }

    @Cacheable(value = "goods_type_list")
    public List<GoodsType> getGoodsTypeList() {
        log.info("通过数据库获取：typeList");
        return goodsMapper.getGoodsTypeList();

    }
}
